<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
function curPage() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"];
 }
 return $pageURL;
}
//link nhom san pham o trang chu
function GetGroupProductLink($group_product)
{
	global $db, $lg, $prefix_url, $FullUrl;
	
	$sql = "select unique_key_$lg as cat_name from categories where id=".$group_product['pid'];
	$r = $db->getRow($sql);
	return $FullUrl . $prefix_url . $r['cat_name'] . "/" . $group_product["unique_key_$lg"] . "/";
}
function GetNameFromCat($cat)
{
	global $db, $lg, $prefix_url, $FullUrl;
	
	$sql = "select name_vn, name_en from categories where unique_key_".$lg . " = '" . $_GET['cat1']."'";
	//echo $sql;
	$title_name = $db->getRow($sql);
	
	return $title_name['name_'.$lg];	
}
function GetCategoryProduct($product, $lg2){

	global $db, $lg, $prefix_url, $FullUrl;
	$id = $product['cid'];
	$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
	$r = $db->getRow($sql);
	if($r && $r['group_name']!= ''){
		$link = $r['group_name'] . "/" . $r['cat_name'] . "/";
	}
	else{
		$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
		$r = $db->getRow($sql);
		$link = $r['cat_name'] . "/";
	}
	return $FullUrl . ($lg=="vn"?"/":"/en/") . $link;
}
function GetOrderStatus($status)
{	
	if($status==0)
		return 'Chưa thanh toán';
	else if($status==1)
		return 'Đã thanh toán';
	else if($status==2)
		return 'Đã xử lý';
}
function GetOrderStatusImage($status)
{	
	if($status==0)
		return 'images/admin/hide.png';
	else if($status==1)
		return 'images/table/paid.png';
	else if($status==2)
		return 'images/table/finished.png';
}
function GetPHPThumb($PathImage, $width, $height)
{	
	global $FullUrl;
	
	$PathImage = GetImage($PathImage);
	$TypeImg = GetFileExtension($PathImage);
	
	$path = $FullUrl . '/phpThumb/phpThumb.php?w=' . $width . '&amp;h=' . $height;
	
	if(strtolower($TypeImg) == 'png')
	{
		$path = $path . '&amp;f=png';
	}
	else
	{
		
	}	
	
	if($PathImage !='')
		$path = $path . "&amp;src=../" . $PathImage;
	else
		return '';
	
	return $path;
}
function GetFileExtension($filename)
{
    $path_info = pathinfo($filename);
	
    return $path_info['extension'];
}
function LanguageAbbreviation()
{
	global $lg;
	$lang = '';
	
	if($lg == 'en')
	 $lang = 'en/';
	
	return $lang;
}

function GenRandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = "";    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}

function GetFullDomain()
{
	return GetDomain(GetFullUrl());
}

function GetDomain($url)
{
	$nowww = preg_replace('[www\.]','',$url);
	$domain = parse_url($nowww);
	if(!empty($domain["host"]))
		return $domain["host"];
	else
		return $domain["path"];
}

function CorrectGoogleCaptcha()
{
	require_once('recaptchalib.php');
	$privatekey = PRIVATE_KEY_GOOGLE;
	$resp = recaptcha_check_answer ($privatekey,
								$_SERVER["REMOTE_ADDR"],
								$_POST["recaptcha_challenge_field"],
								$_POST["recaptcha_response_field"]);
	if (!$resp->is_valid) 
		return false;
	return true;
}
function GetFullUrl(){ 
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; 
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
	
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; 
}

function strleft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2)); 
}

function SafeFormValue($value)
{
	return CleanSQLInjection(trim(isset($_POST[$value])?$_POST[$value]:''));
}

function SafeQueryString($value)
{
	return CleanSQLInjection(trim(isset($_GET[$value])?$_GET[$value]:''));
}

function IsValidEmail($email){
	return filter_var($email, FILTER_VALIDATE_EMAIL);
}

function ShowProvinceList($tbProvinces)
{
	global $db, $provinces;
	
	$sql="select * from ". $tbProvinces ." order by ordering asc";
	
	$provinces = $db->getAll($sql);
}

function EmailExists($email, $tbEmail){
	global $db;
	$sql = "select * from ". $tbEmail ." where email = '$email'";
	
	$c = $db->numRows($db->query($sql));
	if($c < 1)
		return false;
	return true;
}

function ImageCenter($HeightImg, $StandardHeight)
{
    $StandardHeight = $StandardHeight - $HeightImg;
    
    $StandardHeight /= 2;
    
    return $StandardHeight;
}

function ImageCenterHorizontal($WidthImg, $StandardWidth)
{
    $StandardWidth = $StandardWidth - $WidthImg;
    
    $StandardWidth /= 2;
    
    return $StandardWidth;
}

function AlertMessage($mess){
	return '<script language="javascript">alert("'.$mess.'");</script>';	
}

function CleanSQLInjection($string)
{
	$string = strip_tags($string);
	
	if(get_magic_quotes_gpc())  // prevents duplicate backslashes
	{
		$string = stripslashes($string);
	}
	
	$badWords = array("/delete/i", "/update/i","/union/i","/insert/i","/drop/i","/http/i","/--/i");
	$string = preg_replace($badWords, "", $string);
	
	if(!is_numeric($string))
	{
		$string = mysql_real_escape_string($string);
	}
	
	return $string;
}

function ValidString($string)
{
	$string = strtolower($string);
	
	$IllegalCharacters = array("select", "insert", "delete", "update", "create", "remove", "<", ">", "#", "'", "*");
	
	for($i = 0; $i < count($IllegalCharacters); $i++)
	{
		$result = strpos($string, $IllegalCharacters[$i]);
		
		if($result !== false)
		{
			return false;
		}
	}
	
	return true;	

}
function ValidPasswordString($string)
{
	$string = strtolower($string);
	
	$IllegalCharacters = array("select", "insert", "delete", "update", "create", "remove");
	
	for($i = 0; $i < count($IllegalCharacters); $i++)
	{
		$result = strpos($string, $IllegalCharacters[$i]);
		
		if($result !== false)
		{
			return false;
		}
	}
	
	return true;	
}

function GetImage($path)
{
	if(!file_exists($path))
		$path = CST_NO_IMAGE_PATH;
		
	return $path;
}

function RenameFile($filename){

	$filename = str_replace("& ", "", $filename);

	$filename = str_replace(" &", "", $filename);

	$filename = str_replace("&", "", $filename);

	$filename = str_replace(",", "", $filename);

	$filename = str_replace(" - ", "-", $filename);

	$filename = str_replace(" -", "-", $filename);

	$filename = str_replace("- ", "-", $filename);

	$filename = str_replace(" ", "-", $filename);

	return $filename;

}
function StripUnicode($str){
$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
,"ế","ệ","ể","ễ",
"ì","í","ị","ỉ","ĩ",
"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
,"ờ","ớ","ợ","ở","ỡ",
"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
"ỳ","ý","ỵ","ỷ","ỹ",
"đ",
"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
"Ì","Í","Ị","Ỉ","Ĩ",
"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
,"Ờ","Ớ","Ợ","Ở","Ỡ",
"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
"Đ",
"-",":",",",".","?",'/','\\','%','"',"'"
);

$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
,"a","a","a","a","a","a",
"e","e","e","e","e","e","e","e","e","e","e",
"i","i","i","i","i",
"o","o","o","o","o","o","o","o","o","o","o","o"
,"o","o","o","o","o",
"u","u","u","u","u","u","u","u","u","u","u",
"y","y","y","y","y",
"d",
"A","A","A","A","A","A","A","A","A","A","A","A"
,"A","A","A","A","A",
"E","E","E","E","E","E","E","E","E","E","E",
"I","I","I","I","I",
"O","O","O","O","O","O","O","O","O","O","O","O"
,"O","O","O","O","O",
"U","U","U","U","U","U","U","U","U","U","U",
"Y","Y","Y","Y","Y",
"D",
" "," "," "," "," "," "," "," "," "," "
);
return str_replace($marTViet,$marKoDau,$str);
}



function GetCatLink($cat){
	if(1){//$cat['clear_link'] == ""){
		$arrSpace = array("   ", "  "," " );
		$arrSpaceRep = array("-","-","-");
		global $db;
		$comp = "s";
		if($cat['has_child'] != 1){
			$sql = "select * from component where id=".$cat['comp'];
			$r = $db->getRow($sql);
			$comp = $r['do'];
		}
		if($comp == 'main')
			return 'index.html';
		$str = StripUnicode($cat['name_'.$_SESSION['lg']]);
		$link = str_replace($arrSpace,$arrSpaceRep, $str)  . "-" . $comp . "-" . $cat['id'] . ".html#neo";
		
		$arr = array();
		$arr['clear_link'] = $link;
		vaUpdate('categories',$arr,' id='.$cat['id']);
		
		return $link;
	}
	else
		return $cat['clear_link'];
}
function GetArtLink($art, $idActive=0){
	global $db, $lg, $prefix_url;
	$id = $art['cid'];

        if(isset($cat2)){
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		return $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
	else{
		$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
		$r = $db->getRow($sql);
		return $prefix_url . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
}
function GetArtLink2($art, $lg2){

	global $db, $lg, $prefix_url, $cat1, $cat2;
	$id = $art['cid'];
	
	if(isset($cat2)){
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		
		return $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
	else{
		$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
		$r = $db->getRow($sql);
		return $prefix_url . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
}
function GetArtLinkFull($art, $lg2){

	global $db, $lg, $prefix_url, $cat1, $cat2;
	$id = $art['cid'];
	$sql = "SELECT pid FROM categories WHERE id=$id";
	$art_cat = $db->getRow($sql);
	//echo $sql;
	
	if($art_cat['pid'] != 121){
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		//echo $sql;
		return $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
	else{
		$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
		$r = $db->getRow($sql);
		//echo $sql;
		return $prefix_url . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
}
function GetArtLinkSiteList($pid, $lg){

	
	global $db, $lg, $prefix_url, $cat1, $cat2;
	$lg = isset($lg)?$lg:'vn';
	$id_next = $pid;
	$flag = true;
	
	$link_full = '';
	$count = 0;
	
	for($i = 0; $i < 5; $i++)
	{
		$sql = "SELECT * FROM categories WHERE id=". $id_next;
		//echo $sql;
		$art_next = $db->getRow($sql);
		
		$link_full = $art_next['unique_key_'.$lg] .'/' .$link_full;
		$id_next = $art_next['pid'];
		if($id_next == '121' || $id_next == '')
			break;
	}
	
	$rs = $prefix_url . $link_full;
	
	return $rs; 
}
function GetArtLinkCMSFull($art, $lg){

	
	global $db, $lg, $prefix_url, $cat1, $cat2;
	$lg = 'vn';
	$id = $art['cid'];
	$unique_key = $art['unique_key_'.$lg];
	
	$id_next = $art['cid'];
	$flag = true;
	
	$link_full = '';
	$count = 0;
	
	for($i = 0; $i < 5; $i++)
	{
		$sql = "SELECT * FROM categories WHERE id=". $id_next;
		//echo $sql;
		$art_next = $db->getRow($sql);
		
		$link_full = $art_next['unique_key_'.$lg] .'/' .$link_full;
		$id_next = $art_next['pid'];
		if($id_next == '121' || $id_next == '')
			break;
	}
	
	$rs = $prefix_url . $link_full. $unique_key . ".html";
	
	return $rs; 
}
function GetCollLink($art, $idActive=0){
	if($art['clear_link'] == ""){
		$arrSpace = array("   ", "  "," " );
		$arrSpaceRep = array("-","-","-");
		$str = StripUnicode(trim($art['name_'.$_SESSION['lg']]));
		$link = str_replace($arrSpace,$arrSpaceRep, $str) . "-chi-tiet-bo-suu-tap-" . $art['cid'] . "-" . $art['id'] ;
		if($idActive != 0)
			$link .= "-".$idActive;
		$link .= ".html#neo";
		
		$arr = array();
		$arr['clear_link'] = $link;
		vaUpdate('articles',$arr,' id='.$art['id']);
		return $link;
	}
	else
		return $art['clear_link'];
}
function GetProductLink($product, $idActive=0){
	if($product['clear_link'] == ""){
		$arrSpace = array("   ", "  "," " );
		$arrSpaceRep = array("-","-","-");
		$str = StripUnicode($product['name_'.$_SESSION['lg']]);
		$link = str_replace($arrSpace,$arrSpaceRep, $str) . "-chi-tiet-san-pham-" . $product['cid'] . "-" . $product['id'] ;
		if($idActive != 0)
			$link .= "-".$idActive;
		$link .= ".html#neo";
		
		$arr = array();
		$arr['clear_link'] = $link;
		vaUpdate('products',$arr,' id='.$product['id']);
		return $link;
		return $link;
	}
	else
		return $product['clear_link'];
}
function GetProductLink2($product, $lg2){

	global $db, $lg, $prefix_url, $FullUrl, $cat1, $cat2, $cat3;
	$id = $product['cid'];
	if (isset($cat3)) {
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name, c3.unique_key_$lg as cat2_name from categories c1,categories c2, categories c3 where c3.id = $id and c3.pid=c1.id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		return $FullUrl . $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $r['cat2_name'] . "/" . $product["unique_key_$lg"] . ".html";
	}
	else if(isset($cat2)){
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		return $FullUrl . $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $product["unique_key_$lg"] . ".html";
		}
		else{
			$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
			$r = $db->getRow($sql);
			return $FullUrl . $prefix_url . $r['cat_name'] . "/" . $product["unique_key_$lg"] . ".html";
		}
}
function GetImagesLink2($images, $lg2){

	global $db, $lg, $prefix_url, $FullUrl, $cat1, $cat2, $cat3;
	$id = $images['cid'];
	if (isset($cat3)) {
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name, c3.unique_key_$lg as cat2_name from categories c1,categories c2, categories c3 where c3.id = $id and c3.pid=c1.id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		return $FullUrl . $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $r['cat2_name'] . "/" . $images["unique_key_$lg"] . ".html";
	}
	else if(isset($cat2)){
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		return $FullUrl . $prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $images["unique_key_$lg"] . ".html";
		}
		else{
			$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
			$r = $db->getRow($sql);
			return $FullUrl . $prefix_url . $r['cat_name'] . "/" . $images["unique_key_$lg"] . ".html";
		}
}
function GetProductLinkFull($art, $lg2){

	global $db, $lg, $prefix_url, $cat1, $cat2, $FullUrl;
	$id = $art['cid'];
	$sql = "SELECT pid FROM categories WHERE id=$id";
	$art_cat = $db->getRow($sql);
	
	if($art_cat['pid'] != 121){
		$sql = "select c1.unique_key_$lg as cat_name,c2.unique_key_$lg as group_name from categories c1,categories c2 where c1.id=$id and c1.pid=c2.id";
		$r = $db->getRow($sql);
		//echo $FullUrl.$prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
		return $FullUrl.$prefix_url . $r['group_name'] . "/" . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
	}
	else{
		$sql = "select c1.unique_key_$lg as cat_name from categories c1 where c1.id=$id";
		$r = $db->getRow($sql);
		//echo $FullUrl.$prefix_url . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";
		return $FullUrl.$prefix_url . $r['cat_name'] . "/" . $art["unique_key_$lg"] . ".html";

	}
}
function GetIntroLink($id, $lg2){

	global $db, $lg, $prefix_url;
	$sql = "select c1.unique_key_$lg2 as cat_name from categories c1 where c1.id=$id";
	$r = $db->getRow($sql);
	return ($lg2=="vn"?"/":"/en/") . $r['cat_name'] . "/" ;
}
function CreateListRand($total, $min, $max){

	$arr = array();
	if(($max-$min) < $total)
		return null;
		
	while(count($arr) < $total){
		$x = rand($min, $max);
		$flag = 0;
		foreach($arr as $i){
			if($i==$x){
				$flag=1;
				break;
			}
		}
		if($flag == 0)
			$arr[] = $x;
	}
	
	return $arr;
}
function GetLinkProduct($cid){

	global $db;
	$sql = "select id,has_child from categories where id=$cid order by num asc, id asc";
	$cat = $db->getRow($sql);
	
	if($cat == null)
			return "#";
	while($cat['has_child'] == 1){
		
		$sql = "select id,has_child from categories where pid=" . $cat['id'] . " order by num asc, id asc";
		
		$cat = $db->getRow($sql);
		if($cat == null)
			return "#";

	}
	return "index.php?do=products&cid=" . $cat['id'];
}
function GetCatByAlias($alias){
	global $db;
	$sql="select id from categories where alias='$alias'";
	$r = $db->getRow($sql);
	return $r['id'];
}
function GetLink($cat){
	global $db;
	if($cat['has_child'] == 1){ 
		/*$url = $_SERVER["REQUEST_URI"];
		if($url == "/" || strpos($url, "?")<=0)
			return "index.php?expend=".$cat['id'];
		$pos = strpos($url, "expend");
		
		if($pos > 0){
			$url = substr($url,0, $pos) . "expend=".$cat['id'];
			return $url;
		}
		else{
			return $url . '&amp;expend='.$cat['id'];
		}*/
		return "index.php?do=submenu&amp;cid=".$cat['id']."&amp;idActive=".$_GET['idActive'];
	}
	$sql = "select * from component where id=".$cat['comp'];
	$r = $db->getRow($sql);
	return "index.php".$r['front_link']."&amp;cid=".$cat['id']."&amp;idActive=".$_GET['idActive'] . "";
}
function GetLink2($cat){
	global $db;
	if($cat['has_child'] == 1){ 
		return "#";
	}
	$sql = "select * from component where id=".$cat['comp'];
	$r = $db->getRow($sql);
	return "index.php".$r['front_link']."&amp;cid=".$cat['id']."&amp;idActive=".$_GET['idActive'] . (isset($_GET['expend'])?('&amp;expend='.$_GET['expend']):'') . "#neo";
}
function CreateTPLMenu($root, $name){
	$html = "<ul id='$name'>";
	CreateTPLMenuDeQui($root,$html);
	$html .= "</ul>";
	return $html;
}
function CreateTPLMenuDeQui($root,&$html){
	global $db, $idActive;
	$sql = "select * from categories where pid=$root order by num asc";
	$r = $db->getAll($sql);

	if($r){
		foreach($r as $item){

			/*if($item['id'] == 155)
				$link = "index.php?aacecom=submenu&amp;cid=".$item['id'];
			else */
			if($item['comp'] == 0)
				$link = "#";//"index.php?aacecom=submenu&amp;cid=".$item['id'];
			else{
				$sql = "select * from component where id=".$item['comp'];
				$r2 = $db->getRow($sql);
				$link = "index.php".$r2['front_link']."&amp;cid=".$item['id'];
			}
			$html .= "<li";
			if($item['id'] == '156')
				$html .= ' id="end"';
			if($item['id'] == $idActive)
				$html .= ' class="active"';
			$html .= "><a href=\"$link\">".$item['name_'.$_SESSION['lg']]."</a>";
			if($item['has_child']==1){
				$html .= "<ul>";
				$html .= CreateTPLMenuDeQui($item['id'],$html);
				$html .= "</ul>";
			}
			$html .= "</li>";
		}
	}
}
function CreateDataMenu($idMenu, &$Menu, &$MenuLink, &$SubMenu, &$SubMenuLink, &$SubMenu2, &$SubMenu2Link){
	global $db;

	$sql = "select * from categories where pid=$idMenu and showed=1 order by num asc, id asc";
	//echo $sql;
	$Menu = $db->getAll($sql);
	//print_r($Menu);
	if($Menu){
		for($i=0;$i<count($Menu);$i++){
			if($Menu[$i]['has_child'] != '1'){
				$SubMenu[$i] = 0;
				$SubMenu2[$i] = 0;
				$sql = "select * from component where id=".$Menu[$i]['comp'];
				$r = $db->getRow($sql);
				$MenuLink[$i] = "index.php".$r['front_link']."&amp;cid=".$Menu[$i]['id'] . "&idActive=" . $_GET['idActive'];
			}
			else{
				$MenuLink[$i] = "#";
				$SubMenu2[$i] = array();
				$SubMenu2Link[$i] = array();
				$sql = "select * from categories where pid=".$Menu[$i]['id']." and showed=1 order by num asc, id asc";
				$subs = $db->getAll($sql);
				if($subs){
					$temp = array();
					for($j=0;$j<count($subs);$j++){
						if($subs[$j]['has_child'] != '1'){
							$SubMenu2[$i][$j] = 0;
							$sql = "select * from component where id=".$subs[$j]['comp'];
							$r = $db->getRow($sql);
							$temp[$j] = "index.php".$r['front_link']."&amp;cid=".$subs[$j]['id'] . "&idActive=" . $_GET['idActive'];
						}
						else{
							$temp[$j] = "#";
							$sql = "select * from categories where pid=".$subs[$j]['id']." and showed=1 order by num asc, id asc";
							$SubMenu2[$i][$j] = $db->getAll($sql);
							for($u=0;$u<count($SubMenu2[$i][$j]);$u++){
								$sql = "select * from component where id=".$SubMenu2[$i][$j][$u]['comp'];
								$r = $db->getRow($sql);
								$SubMenu2Link[$i][$j][$u] = "index.php".$r['front_link']."&amp;cid=".$SubMenu2[$i][$j][$u]['id'] . "&idActive=" . $_GET['idActive'];
							}
						}
					}
					$SubMenu[$i] = $subs;
					$SubMenuLink[$i] = $temp;
				}
			}
		}
	}
}
function getTitleBar($cid, $rootOfCat, $sep){
	$sep = ' > ';
	$tilte = "";
	global $db;
	$temp = $cid;
	$arr = array();
	$sql = "select * from categories where id=$temp";
	$r = $db->getRow($sql);
	//$tilte .= $r["name_".$_SESSION['lg']];
	$i=0;
	$arr[$i] = $r;
	$i++;
	$temp = $r["pid"];
	while($temp != 121){
		if($temp == 1)
			break;
		$sql = "select * from categories where id=$temp";
		$r = $db->getRow($sql);
		//$tilte = $r["name_".$_SESSION['lg']] . $sep . $tilte;
		$arr[$i] = $r;$i++;
		$temp = $r["pid"];
	}
	$xxx = $i-2;
	if($arr[$i-1]["id"] != 345)
		$tilte = '<a title="'.$arr[$i-1]["name_".$_SESSION['lg']].'" href="' . "Javascript:history.go(-1)"/*GetCatLink($arr[$i-1])*/ . '">' . $arr[$i-1]["name_".$_SESSION['lg']] . '</a>';
	else{
		$tilte = '<a title="'.$arr[$i-2]["name_".$_SESSION['lg']].'" href="' . "Javascript:history.go(-1)"/*GetCatLink($arr[$i-2])*/ . '">' . $arr[$i-2]["name_".$_SESSION['lg']] . '</a>';
		$xxx = $i-3;
	}
	for($j = $xxx; $j>=0; $j--){
		$tilte .= $sep . '<a title="'.$arr[$j]["name_".$_SESSION['lg']].'" href="' . "Javascript:history.go(-1)"/*GetCatLink($arr[$j])*/ . '">' . $arr[$j]["name_".$_SESSION['lg']] . '</a>';
	}
/*	print_r($arr);
	echo $i;
	echo $tilte;
	print_r($arr[$i-1]);*/
	return $tilte;
}
function getSimpleTitle($cid){
	$tilte = "";
	global $db;
	$sql = "select name_vn, name_en from categories where id=" . $cid;
	$r = $db->getRow($sql);
	if($r)
		return $r['name_' . $_SESSION['lg']];
	else
		return "";
}
function SubStrEx($str, $length){
	if(strlen($str) <= $length)
		return $str;

	$pos = strpos($str, " " , $length);
	if($pos >0 )
		return substr($str, 0, $pos) . ' ...';
	else
		return $str;
}
function FormatDate($date){
	$day = substr($date,8,2);
	$mon = substr($date,5,2);
	$year = substr($date,0,4);
	return $day.'.'.$mon.'.'.$year;
}
function sql_injection($str){
	if(!ereg('[\\\'\"<>:;,=&]', $str))
	{
		return $str;
	}else
	{
		echo '<script>window.location="index.php";</script>';
	}
}

function insert_id(){
	$this->insert_id = mysql_insert_id();
	return $this->insert_id;
}

function plpage_seo($sqlstmt, $page, $set_per_page)
{
	global $db,$do,$set_per_page, $prefix_url, $lg, $FullUrl;
	$trang = paging;
	$cat1 = SafeQueryString('cat1');
	$cat2 = SafeQueryString('cat2');
	$unique_key = SafeQueryString('unique_key');
	
	if($cat2){
		$my_url = "$prefix_url$cat1/$cat2";
	}
	else
		$my_url = "$prefix_url$cat1";
		
	if (!$set_per_page) $set_per_page=$set_per_page;

	$result = $db->query($sqlstmt);
	$rows = $db->numRows($result);
	$p=ceil($rows/$set_per_page);

	$str='';
	if($p<=1) return $str;

	if ($p) $str = '<div class="wp-pagenavi phan-trang"><p class=""><ul>';

	$first = 0; $last=0;
	$j=1; $k=$p;

	if ($page>1){
		$pageprev=$page-1;
		
		if($unique_key){
			$str .= '<li class="First"><a href="'. $FullUrl . $my_url . ($pageprev==1?("/".$unique_key.".html"):("/".$unique_key."-$trang-".$pageprev.".html")) ."\" class=\"PrevBtn\">Prev</a></li>";}
		else {
			$str .= '<li class="First"><a href="'. $FullUrl . $my_url . '/' ."\" class=\"PrevBtn\">First</a></li>";
			$str .= '<li class="First"><a href="'. $FullUrl . $my_url . ($pageprev==1?'':("-$trang-".$pageprev)) . '/' ."\" class=\"PrevBtn\">Prev</a></li>";}
	}
	
	for($i=($page-2>1)?$page-2:1;$i<=$page+2 && $i<=$k;$i++){
		$first = ' class="First"';
		if($i>1)
			$first = '';
		if ($i==$page)
			$str .= "<li$first><a href='#' class='current'>$i</a></li>";
		else {
			if($unique_key)
				$str .= "<li$first><a href=\"". $FullUrl . $my_url . ($i==1?("/".$unique_key.".html"):("/".$unique_key."-$trang-".$i . ".html")) ."\">$i</a></li>";
			else
				$str .= "<li$first><a href=\"". $FullUrl . $my_url . ($i==1?'':("-$trang-".$i)) . '/' ."\">$i</a></li>";
		}
	}
	
	if ($page<$p){
		$pagenext=$page+1;
		$query_str = explode("&page=", $_SERVER['QUERY_STRING']);
		
		if($unique_key) {
			$str .= '<li><a href="'. $FullUrl . $my_url . "/".$unique_key . "-$trang-" . $pagenext . ".html" ."\" class=\"NextBtn\">Next</a></li>";
		}
		else{
			$str .= '<li><a href="'. $FullUrl . $my_url . "-$trang-" . $pagenext . "/" . "\" class=\"NextBtn\">Next</a></li>";
			$str .= '<li><a href="'. $FullUrl . $my_url . "-$trang-" . $p . "/" . "\" class=\"NextBtn\">Last</a></li>";
		}
	}

	$str .= "</ul></p></div>";
	return $str;
}
function plpage($sqlstmt,$page,$set_per_page)
{
	global $db,$do,$set_per_page;

	if (!$set_per_page) $set_per_page=$set_per_page;

	$result = $db->query($sqlstmt);
	$rows = $db->numRows($result);
	$p=ceil($rows/$set_per_page);

	$str='';
	if($p<=1) return $str;

	if ($p) $str = '<ul class="pagination">';

	$first = 0; $last=0;
	$j=1; $k=$p;
	if (false&&$first){
			$query_str = explode("&page=", $_SERVER['QUERY_STRING']);
	}
	if ($page>1){
			$pageprev=$page-1;
			$query_str = explode("&page=", $_SERVER['QUERY_STRING']);
			$str .= '<li class="First"><a href="'.$_SERVER['SCRIPT_NAME'].'?'.$query_str[0]."&page=$pageprev\">Prev </a></li>";
	}
	
	
	for($i=1;$i<=$k;$i++){
		$first = ' class="First"';
		if($i>1)
			$first = '';
		if ($i==$page)
			$str .= "<li$first><a href='#' class='active'>$i</a></li>";
		else {
			$query_str = explode("&page=", $_SERVER['QUERY_STRING']);
			$str .= "<li$first><a href=\"".$_SERVER['SCRIPT_NAME']."?".$query_str[0]."&page=$i\">$i</a></li>";
		}
	}
	
	if ($page<$p){
			$pagenext=$page+1;
			$query_str = explode("&page=", $_SERVER['QUERY_STRING']);
			$str .= '<li><a href="'.$_SERVER['SCRIPT_NAME'].'?'.$query_str[0]."&page=$pagenext\"> Next</a></li>";
	}
	
	if (false&&$last){
		$query_str = explode("&page=", $_SERVER['QUERY_STRING']);
	}
	$str .= '</u>';
	return $str;
}
function sqlmod($sqlstmt,$page,$set_per_page){ //modified sql query danh cho phan trang
	global $set_per_page;
	$from = ($page-1)*$set_per_page;
	return $sqlstmt." LIMIT ".$from." ,".$set_per_page;
}

function page_transfer2($page="index.php")
{
     $page_transfer = $page;
     include("./kam/transfer2.tpl");
     exit();
}
function page_transfer_admin($msg,$page){
     $showtext = $msg;
     $page_transfer = $page;
     include("./kam/transfer.tpl");
     exit();
}

function page_transfer($msg,$page){
     $showtext = $msg;
     $page_transfer = $page;
     include("./kam/transfer.tpl");
     exit();
}

function cut_string($str,$len,$more){
   if ($str=="" || $str==NULL) return $str;
   if (is_array($str)) return $str;
   $str = trim($str);
   if (strlen($str) <= $len) return $str;
   $str = substr($str,0,$len);
   if ($str != "") {
        if (!substr_count($str," ")) {
             if ($more) $str .= " ...";
             return $str;
            }
        while(strlen($str) && ($str[strlen($str)-1] != " ")) {
                $str = substr($str,0,-1);
            }
            $str = substr($str,0,-1);
            if ($more) $str .= " ...";
        }
        return $str;
}
function IsFirefox(){
	return strrpos($_SERVER['HTTP_USER_AGENT'],"Firefox")>0;
}
function IsIE7(){
	return strrpos($_SERVER['HTTP_USER_AGENT'],"MSIE 7")>0;
}
function IsIE6(){
	return strrpos($_SERVER['HTTP_USER_AGENT'],"MSIE 6")>0;
}
function fixWidth($img,$w){
	$arr = getimagesize($img);
	return $arr[0]>$w?$w:$arr[0];
}
function fixHeight($img,$h){
	$arr = getimagesize($img);
	return $arr[1]>$h?$h:$arr[1];
}

function getApproriateSize($img,$w,$h)
{
	if(file_exists($img))
	{
		$sizes = getimagesize($img);
		$new_sizes = array();
		
		if($sizes)
		{
			$width = $sizes[0];
			$height = $sizes[1];
			
			$scale = $width/$height;
			
			if($scale > 1 || $scale < 1)
			{
				if($width > $w)
					$width = $w;
				if($height > $h)
					$height = $h;
			}
			else
			{
				if($width > $w)
					$width = $w;
				
				$width = $w;
				$height = $w;
			}
			
			$new_sizes[0] = $width;
			$new_sizes[1] = $height;
		}
	}
	
	return $new_sizes;
}

function fixSize($img,$w,$h){
	$arr = getimagesize($img);
	if($arr){
		$kq = array();
		$scalew = $w/$arr[0];
		$scaleh = $h/$arr[1];
		$scale = $scalew<$scaleh?$scalew:$scaleh;
		
		if($scale<1){
			$kq['width'] = (int)($arr[0]*$scale);
			$kq['height'] = (int)($arr[1]*$scale);
		}
		else
		{
			$kq['width'] = $arr[0];
			$kq['height'] = $arr[1];
		}
		return $kq;
	}
	else
		return null;
}
function fixOutSize($img,$w,$h){
	$arr = getimagesize($img);
	
	if($arr){
		//echo $img; 
		$kq = array();
		$scalew = $arr[0]/$w;
		$scaleh = $arr[1]/$h;
		$scale = $scalew<$scaleh?$scalew:$scaleh;
		
		if($scale* $h < $arr[1]){
			$kq['width'] = $w;
			$kq['height'] = (int)($arr[1]/$scale);
			
		}
		else{
			$kq['width'] = (int)($arr[0]/$scale);
			$kq['height'] = $h;
		
		}
		/*echo "W: " . $w . "H: " . $h;
		echo "W: " . $kq['width'] . "H: " . $kq['height'];*/
		return $kq;
	}
	else
		return null;
}
function CorrectFileTypes($type){
	$type = strtolower($type);
	if($type!=".jpg" && $type!=".bmp" && $type!=".gif" && $type!=".png" && $type!=".rar" && $type!=".doc" && $type!=".zip" && $type!=".rar" && $type!=".flv" && $type!=".mp3" && $type!=".wma" && $type!=".avi" && $type!=".wmv")
		return false;
	return true;
}

function CheckUpload($type){
	$type = strtolower($type);
	if($type!=".jpg" && $type!=".bmp" && $type!=".gif" && $type!=".png" && $type!=".rar" && $type!=".doc" && $type!=".zip" && $type!=".rar" && $type!=".flv" && $type!=".mp3" && $type!=".wma" && $type!=".avi" && $type!=".wmv")
		return false;
	return true;
}

function selfURL() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } 

?>